Stygian.influencer = Stygian.influencer or {}
function Stygian_getRoomItems()
  send("ih")
  SDebug("influencer", "Ok, checking the room items!")
  Stygian.roomItems = {}
  Stygian.roomItemTypes = {}
  Stygian.ihTriggerId = tempLineTrigger(0,200,[[Stygian_readRoomItems()]])
  SDebug("influencer", "Ok, we just setup the trigger for readRoomItems")
end

function Stygian_readRoomItems()
  selectString(line,1)
  replace("")
  SDebug("influencer", line)
  local item = line:match([[".+"]])
  if item == nil then 
    SDebug("influencer", "This is not an item, so we stop looking")
    killTrigger(Stygian.ihTriggerId)
    Stygian.ihTriggerId = nil
    send("config pagelength 40")

  else
    item = item:match("%w+%d+")
    SDebug("influencer", "The item is " .. item)
    local type = item:match("%a+")
    SDebug("influencer", "It is of type " .. type)
    Stygian.roomItemTypes[type] = Stygian.roomItemTypes[type] or 0
    Stygian.roomItemTypes[type] = (Stygian.roomItemTypes[type] + 1) or 1
    table.insert(Stygian.roomItems, item)
    deleteLine()
  end
end


function Stygian_influenceComplete()
  if Stygian.influencer.massInfluence and #Stygian.targetList > 0 then 
    SDebug("influencer", "we're mass influencing, and there are more targets")
    Stygian.influencer.target = table.remove(Stygian.targetList)
    SDebug("influencer", "Next target is " .. Stygian.influencer.target)
    target = Stygian.influencer.target
  else
    SDebug("influencer", "alright, nothing left to influence")
    Stygian.influencer.actionCancelled = true
    Stygian.influencer.massInfluence = false
    target = ""
  end
end

function Stygian_extractEntityType(entity)
  Stygian.targetList = {}
  SDebug("influencer", "We're now pulling all items of type " .. entity .. " from the room item list")
  for i,item in ipairs(Stygian.roomItems) do
    temp = item:match(entity)
    if temp ~= nil then table.insert(Stygian.targetList, item) end
  end
  if #Stygian.targetList > 0 then
    SDebug("influencer", "There are targets!")
    Stygian.influencer.target = table.remove(Stygian.targetList)
    SDebug("influencer", "And our current target is: " .. Stygian.influencer.target)
    target = Stygian.influencer.target
  else 
    Stygian.influencer.actionCancelled = true
    Stygian.influencer.massInfluence = false
    secho("Either you've failed to provide a type of target that's in the room, or you've muffed the syntax")
  end
end

function Stygian_onInfluenceContinue()
  SDebug("influencer", "Time to influence again")
  if Stygian.influencer.target == nil then 
    SDebug("influencer", "No target to influence. Time to stop influencing") 
    return 
  end
	SDebug("influencer", "We have a target, it is " .. Stygian.influencer.target)
	if Stygian.influencer.actionCancelled == false then
	  SDebug("influencer", "The action has not been canceled")
		if Stygian.curEgo <= (Stygian.maxEgo / 4) then
		  SDebug("influencer", "Time to run, less than a quarter ego remaining")
			secho("FLEEING")
			send(Stygian.influencer.actionExit)
		else
		  SDebug("influencer", "No need to flee.")
      if Stygian.influencer.actionNext > #Stygian.influencer.actionList then
        SDebug("influencer", "Ok, our next action is greater than 3, time to go back to 1")
			  Stygian.influencer.actionNext = 1
			end
      Stygian.influencer.action = "INFLUENCE "..Stygian.influencer.target.." WITH "..Stygian.influencer.actionList[Stygian.influencer.actionNext]
      send(Stygian.influencer.action)
			Stygian.influencer.actionNext = Stygian.influencer.actionNext + 1
			Stygian_chaser([[Stygian_onInfluenceContinue()]], {consumesBal = false})
		end
	end
end
